package com.kgc.sbt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.kgc.sbt.api.HotWordApi;
import com.kgc.sbt.api.ShowSearchApi;
import com.kgc.sbt.api.TrackApi;
import com.kgc.sbt.domain.Goods;
import com.kgc.sbt.domain.GoodsFroms;
import com.kgc.sbt.util.PageSupport;
import com.kgc.sbt.util.RedisUtils;
import com.kgc.sbt.util.RequestResult;
import com.kgc.sbt.util.ResultBuildUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLOutput;
import java.util.List;
import java.util.Set;

/**
 * Created on 2021-06-22.
 * <p>
 * Author: a123
 * <p>
 * Description: 商品操作入口
 */
@RestController
@Api(tags = "商品展示搜索操作入口，版本号：1.0.0")
@Slf4j
public class ShowSearchController {

    @Reference
    private ShowSearchApi showSearchApi;

    @Reference
    private HotWordApi hotWordApi;

    @Reference
    private TrackApi trackApi;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * @author a123
     * @date 2021-06-22
     * @return
     * @description 商品展示
     */
    @GetMapping("/showGoods")
    @ApiOperation(value = "新品展示列表",notes = " ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",required = true,value = "页码"),
            @ApiImplicitParam(name = "pageSize",required = true,value = "页面容量")

    })
    @ApiResponses({
            @ApiResponse(code = 0001,message = "无任何商品，数据为空" )
    })
    public RequestResult<PageSupport<GoodsFroms>> showGoods(@RequestParam Integer pageNo, @RequestParam Integer pageSize){

        if(showSearchApi.showGoods(pageNo,pageSize) != null){
            return ResultBuildUtil.success(showSearchApi.showGoods(pageNo,pageSize));
        }
        return ResultBuildUtil.fail("0001","无任何商品，数据为空");
    }


    /**
     * @author a1231
     * @date 2021-06-22
     * @return
     * @description 根据商品名称模糊查询
     */
    @GetMapping("/selectGoodsByName")
    @ApiOperation(value = "根据商品名称模糊查询",notes = "不输入时，根据热词查")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "商品关键字")
    })
    @ApiResponses({
            @ApiResponse(code = 0002,message = "无类似商品，数据为空" )
    })
    public RequestResult<List<GoodsFroms>> selectGoodsByName(@RequestParam(required = false) String name){


        if(name.isEmpty()){
            Set<Object> set = redisUtils.reverseRange("searchHotWord",0,0);
            String name1 = set.toString();
            log.info(name1);
            return ResultBuildUtil.success(showSearchApi.selectHot(name1.substring(1,2)));
        }

        // 判断是否为空
        if(!showSearchApi.selectGoodsByName(name).isEmpty()){
            //增加热词记录
            if(!name.isEmpty()){
                hotWordApi.searchZincrby(name);
            }

            return ResultBuildUtil.success(showSearchApi.selectGoodsByName(name));
        }

        return ResultBuildUtil.fail("0002","无类似商品，数据为空");
    }


}
